import os

target_ext = ('.cpp', '.h', '.qml', '.ui', '.js')


def is_target_file(filename):
    return os.path.splitext(filename)[1] in target_ext


def is_generated(root: str, filename: str):
    return root.lower().find("generated") != -1 \
           or root.lower().find("qtbuild\\") != -1 \
           or root.lower().find("qt-build\\") != -1 \
           or root.lower().find("googletest\\") != -1 \
           or root.lower().find("release\\") != -1\
           or root.lower().find("node_modules\\") != -1\
           or root.lower().find("ionicui") != -1\
           or filename.lower().find("moc_") != -1\
           or filename.lower().find("uic_") != -1


def filter_targets(root, filename):
    # print("file :", filename)
    # print("is target : ", is_target_file(filename))
    # print("is generated : ", is_generated(root, filename))
    return is_target_file(filename) and not is_generated(root, filename)


def code_length(root, filename):
    path = os.path.join(root, filename)
    n = 0
    try:
        with open(path, mode="rb") as f:
            for l in f:
                n += 1
    except Exception as e:
        print(e)

    return n


def main():
    nl = 0
    for root, dirs, files in os.walk('.'):
        for filename in files:
            if filter_targets(root, filename):
                path = os.path.join(root, filename)
                len = code_length(root, filename)
                nl += len
                print(path, len)
    print (nl)


if __name__ == '__main__':
    main()
